package com.interview.customer.controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.interview.contacts.entity.Contacts;
import com.interview.contacts.entity.ContactsListForm;
import com.interview.contacts.query.ContactsConditionQuery;
import com.interview.contacts.respository.ContactsRespository;
import com.interview.core.page.Page;
import com.interview.customer.entity.Customer;
import com.interview.customer.entity.CustomerListForm;
import com.interview.customer.query.CustomerConditionQuery;
import com.interview.customer.service.CustomerService;

@Controller
@RequestMapping(value = "customer")
public class CustomerController {
	@Resource
	private CustomerService customerService;

	@RequestMapping(value = "{id}/view", method = RequestMethod.GET)
	public String customerView(@PathVariable String id, ModelMap map) {
		Customer customer = customerService.findOneCustomer(id);
		List<Contacts> contacts = customer.getContacts();
		map.put("customer", customer);
		map.put("list", contacts);

		return "customer/view";
	}

	@RequestMapping(value = "page", method = RequestMethod.GET)
	public String page(ModelMap map, CustomerConditionQuery conditionQuery) {

		Page page = customerService
				.findByCustomersCustomerConditionQuery(conditionQuery);

		Integer total = page.getTotal();
		Integer pageIndex = page.getPageIndex();
		Integer pageSize = page.getPageSize();

		Integer totalPage = total / pageSize;

		if (total % pageSize != 0) {
			totalPage = totalPage + 1;
		}

		if (totalPage <= 0) {
			totalPage = 1;

		}

		if (pageIndex >= totalPage & totalPage != 0) {
			pageIndex = totalPage - 1;
		}

		List<Customer> customers = page.getList();

		map.put("pageIndex", pageIndex);
		map.put("pageSize", pageSize);
		map.put("totalPage", totalPage);
		map.put("list", customers);
		map.put("conditionQuery", conditionQuery);
		return "customer/page";
	}

	@RequestMapping(value = "add", method = RequestMethod.GET)
	public String toAdd(ModelMap map) {
		List<Contacts> contacts = new ArrayList<Contacts>();
		for (int i = 0; i < 10; i++) {
			contacts.add(null);

		}
		map.put("list", contacts);
		return "customer/add";
	}

	@RequestMapping(value = "add", method = RequestMethod.POST)
	public String add(ModelMap map, Customer customer, ContactsListForm contacts) {

		customerService.saveCustomerAndContactsListForm(customer, contacts);
		return "redirect:/customer/page";
	}

	@RequestMapping(value = "{id}/edit", method = RequestMethod.GET)
	public String toEidt(ModelMap map, @PathVariable String id,
			ContactsConditionQuery conditionQuery) {

		Customer customer = customerService.findOneCustomer(id);

		List<Contacts> contacts = null;

		if (conditionQuery.getCus_id() != null) {

			Page page = customerService
					.findContactsByContactsConditionQuery(conditionQuery);
			contacts = page.getList();
			Integer total = page.getTotal();
			Integer pageIndex = page.getPageIndex();
			Integer pageSize = page.getPageSize();

			Integer totalPage = total / pageSize;

			if (total % pageSize != 0) {
				totalPage = totalPage + 1;
			}

			if (totalPage <= 0) {
				totalPage = 1;

			}

			if (pageIndex >= totalPage & totalPage != 0) {
				pageIndex = totalPage - 1;
			}
			map.put("pageIndex", pageIndex);
			map.put("pageSize", pageSize);
			map.put("totalPage", totalPage);

		} else {
			contacts = customer.getContacts();
		}

		map.put("customer", customer);
		map.put("list", contacts);
		return "customer/edit";
	}

	@RequestMapping(value = "edit", method = RequestMethod.PUT)
	public String edit(ModelMap map, Customer customer,
			ContactsListForm contacts) {
		if (contacts.getContacts() != null) {
			customerService.updateCustomerAndContactsListForm(customer,
					contacts);
		} else {

			customerService.updateCustomer(customer);
		}

		return "redirect:/customer/page";
	}

	@RequestMapping(value = "{id}", method = RequestMethod.DELETE)
	public String delete(ModelMap map, @PathVariable String id) {
		customerService.deleteCustomer(id);
		return "redirect:/customer/page";
	}

	@RequestMapping(value = "childpage", method = RequestMethod.GET)
	public String childpage(ModelMap map, CustomerConditionQuery conditionQuery) {
		
		page(map, conditionQuery);

		return "customer/childpage";
	}

	// -----------------------------------------------------------------------------------
	/*
	 * 
	 * 新建 一个对象 内属性 为一个list存储 一个Entity 利于 spring-Controller信息对话，一次获取对个对象
	 * 
	 * @RequestMapping public String testList(ModelMap map) { List<Customer>
	 * customers = customerService.findAll(); map.put("list", customers); return
	 * "customer/test"; }
	 * 
	 * @RequestMapping(value = "testList", method = RequestMethod.POST) public
	 * void testList(CustomerListForm customers) { for (Customer customer :
	 * customers.getCustomers()) { System.out.println(customer.getId() +
	 * "-----------------------" + customer.getFax()); } }
	 */
}
